package com.yileiqian.alg;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class DrawTest {

	public static final int THRESHOLD = 16;

	public static List<HashMap<String, List<String>>> maps = new ArrayList<HashMap<String, List<String>>>();

	public static void buildLib() {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(
					"input.txt"));
			HashSet<String> set = new HashSet<String>();

			String buffer = reader.readLine();
			String[] sbs = null;

			while (buffer != null) {
				sbs = buffer.split(" ");
				if (sbs.length > 0 && isEnglish(sbs[0])) {
					set.add(sbs[0]);
				}
				buffer = reader.readLine();
			}

			for (int i = 0; i < THRESHOLD; i++) {
				maps.add(new HashMap<String, List<String>>());
			}

			for (String s : set) {
				if (s.length() < THRESHOLD) {
					String tmp = getSortedString(s);
					HashMap<String, List<String>> map = maps.get(s.length());

					List<String> li = map.get(tmp);
					if (li == null) {
						li = new ArrayList<String>();
						li.add(s);
						map.put(tmp, li);
					} else {
						li.add(s);
					}

				}
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String getSortedString(String word) {
		word = word.toLowerCase();
		int[] count = new int[26];
		for (int i = 0; i < word.length(); i++) {
			count[(int) (word.charAt(i) - 97)]++;
		}
		StringBuffer buffer = new StringBuffer();

		for (int i = 0; i < count.length; i++) {
			for (int j = 0; j < count[i]; j++) {
				buffer.append((char) (97 + i));
			}
		}

		return buffer.toString();
	}

	public static boolean isEnglish(String word) {
		word = word.toLowerCase();
		for (int i = 0; i < word.length(); i++) {
			if (word.charAt(i) < 97 || word.charAt(i) > 122) {
				// System.err.println(word);
				return false;
			}
		}
		return true;
	}

	public void cal(String resource, int num) {
		resource = getSortedString(resource);
		HashMap<String, List<String>> map = maps.get(num);

		for (int i = 0; i < resource.length() - num; i++) {
			
		}

	}

	public static void main(String[] args) {
		buildLib();
	}
}
